﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eVoteClient.util;

namespace eVoteClient
{
    public partial class Home : Form
    {
        List<string> openPollsList = new List<string>();
        List<string> closePollsList = new List<string>();

        List<string> cids = new List<string>();
        List<string> oids = new List<string>();

        public Home()
        {
            
            InitializeComponent();
            
            if (!global.isAdmin)
                button1.Visible = false;
            var socket = global.socket;

            socket.writer.WriteLine("OPN_POL_REQ\n");
            socket.writer.Flush();
            string reply = socket.reader.ReadLine();

            

            string [] code = reply.Split('#');

            for (int i = 2; i < code.Length; i += 2)
            {
                openPollsList.Add(code[i]);
                oids.Add(code[i - 1]);
            }

            openListBox.DataSource = openPollsList;



            socket.writer.WriteLine("CLS_POL_REQ\n");
            socket.writer.Flush();
            string reply2 = socket.reader.ReadLine();
            string[] code2 = reply2.Split('#');

            for (int i = 2; i < code2.Length; i += 2)
            {
                closePollsList.Add(code2[i]);
                cids.Add(code2[i - 1]);
            }

            closedListBox.DataSource = closePollsList;
        }

        private void openListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
      
        private void closedListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void closedListBox_DoubleClick(object sender, EventArgs e)
        {
            new LoadPoll(Int32.Parse(cids[closedListBox.SelectedIndex])).Show();
            this.Hide();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }


        private void openListBox_DoubleClick(object sender, EventArgs e)
        {
            new LoadPoll(Int32.Parse(oids[openListBox.SelectedIndex])).Show();
            this.Hide();
        }
        public void ThreadProc1()
        {
            Application.Run(new LoadPoll(Int32.Parse(oids[openListBox.SelectedIndex])));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            new CreatePoll().Show();
            this.Hide();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new Login().Show();
            this.Hide();
        }

        private void Home_Load(object sender, EventArgs e)
        {
            this.Location = new Point(350, 200);
            this.Size = new Size(572,324);
        }
    }
}
